from sqlalchemy import insert
from src.database import session_factory, engine
from src.models import Base, Authors, Books, Students, ReceivingBooks


class SyncOrm:
    @staticmethod
    def create_tables():
        Base.metadata.drop_all(engine)
        Base.metadata.create_all(engine)

    @staticmethod
    def insert_authors():
        with session_factory() as session:
            author_1 = Authors(first_name="Иван", last_name="Бунин")
            author_2 = Authors(first_name="Лев", last_name="Толстой")
            author_3 = Authors(first_name="Михаил", last_name="Булгаков")
            session.add_all([author_1, author_2, author_3])
            session.commit()

    @staticmethod
    def insert_books():
        with session_factory() as session:
            book_1 = Books(title="Антоновские яблоки", author_id=1)
            book_2 = Books(title="Война и мир", author_id=2, count=3)
            book_3 = Books(title="Собачье сердце", author_id=3, count=7)
            session.add_all([book_1, book_2, book_3])
            session.commit()

    @staticmethod
    def insert_students():
        with session_factory() as session:
            st_1 = Students(
                first_name="Иван", last_name="Иванов", phone="+79021122334", email="ivan@mail.ru"
            )
            st_2 = Students(
                first_name="Василий", last_name="Васильев", phone="+79021122335", email="vasya@mail.ru"
            )
            st_3 = Students(
                first_name="Сидор", last_name="Сидоров", phone="+79021122336", email="sidor@mail.ru"
            )
            session.add_all([st_1, st_2, st_3])
            session.commit()

    @staticmethod
    def insert_take_books():
        with session_factory() as session:
            take_book = ReceivingBooks(student_id=1, book_id=2)
            take_book_2 = ReceivingBooks(student_id=2, book_id=1)
            session.add_all([take_book, take_book_2])
            session.commit()
